package com.xhu.dao.impl;

import com.xhu.bean.pojo.saleperform;
import com.xhu.dao.SgDao;
import com.xhu.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SgDaoImpl implements SgDao {
    @Override
    public List<saleperform> queryPage(Integer start, Integer limit) {
        List<Object> parms=new ArrayList<>();

        StringBuffer sql=new StringBuffer(" select * from saleperform ");
        sql.append(" limit ?,? ");
        parms.add(start);
        parms.add(limit);

        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        List<saleperform> list=null;
        try {
            list=queryRunner.query(connection,sql.toString(),parms.toArray(),new BeanListHandler<>(saleperform.class));
        } catch (SQLException e) {
            System.out.println("list中的sql出现问题");
            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
        return list;
    }

    @Override
    public int add(saleperform saleperform) {
        String sql="INSERT INTO " +
                " saleperform (emp_name,perform_level,dept_name,emp_count," +
                " current_name,saleperform_code,des,create_time ) " +
                " VALUE(?,?,?,?,?,?,?,?) ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,
                    saleperform.getEmp_name(),
                    saleperform.getPerform_level(),
                    saleperform.getDept_name(),
                    saleperform.getEmp_count(),
                    saleperform.getCurrent_name(),
                    saleperform.getSaleperform_code(),
                    saleperform.getDes(),
                    saleperform.getCreate_time());
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public int del(String saleperform_id) {
        String sql=" DELETE FROM saleperform " +
                " WHERE saleperform_id=? ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql,saleperform_id);
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public int upData(saleperform saleperform, String saleperform_id) {
        String sql=" UPDATE saleperform " +
                " set saleperform_code=?,emp_name=?,dept_name=?,perform_level=?,des=? " +
                " where saleperform_id=? ";
        Connection connection=ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        try {
            return queryRunner.update(connection,sql, saleperform.getSaleperform_code(),
                                                    saleperform.getEmp_name(),
                                                    saleperform.getDept_name(),
                                                    saleperform.getPerform_level(),
                                                    saleperform.getDes(),saleperform_id);
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
    }

    @Override
    public List<saleperform> queryPage(Integer start, Integer limit, String search_name) {
        List<Object> parms=new ArrayList<>();

        StringBuffer sql=new StringBuffer(" select * from saleperform where emp_name like '%" +search_name+ "%'");
        sql.append(" limit ?,? ");
        parms.add(start);
        parms.add(limit);

        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();

        List<saleperform> list=null;
        try {
            list=queryRunner.query(connection,sql.toString(),parms.toArray(),new BeanListHandler<>(saleperform.class));
        } catch (SQLException e) {
            System.out.println("list中的sql出现问题");
            throw new RuntimeException(e);
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
        return list;
    }
}
